<template>
  <div class="user-page">
    <div class="bg">
      <img :src="srcUrl" alt="" />
      <div class="login">
        <div class="picture">
          <img
            src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fup.enterdesk.com%2Fedpic%2F45%2F06%2F08%2F45060847047b6c7378c454dff41f5d53.jpg&refer=http%3A%2F%2Fup.enterdesk.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1670499325&t=d2e4f2298c9bf3041cb5faf36e8f540d"
            alt=""
          />
        </div>
        <div class="text">
          <p>{{nickname}}</p>
          <van-button type="primary" color="#21b97a"  to="/login" size="small" v-if="show">去登录</van-button>
          <van-button type="primary" color="#21b97a"  @click="clickFn" size="small" v-else>退出登录</van-button>
          <div class="edit" v-show="showEdit"><span>编辑个人资料</span><span class="angle"></span></div>
        </div>
      </div>
    </div>
    <div class="list">
      <van-grid :column-num="3" :border="false" >
        <van-grid-item icon="star-o" text="我的收藏" to="/collect"/>
        <van-grid-item icon="wap-home-o" text="我的出租" to="/myrent" />
        <van-grid-item icon="clock-o" text="看房记录" />
        <van-grid-item icon="notes-o" text="成为房主" />
        <van-grid-item icon="user-o" text="个人资料" />
        <van-grid-item icon="service-o" text="联系我们" />
      </van-grid>
    </div>
    <div class="more">
      <img src="http://liufusong.top:8080/img/profile/join.png" alt="">
    </div>
  </div>
</template>

<script>
import { getUserInfo, logout } from '@/api/user'
import { getToken, delToken } from '@/utils/storage'
export default {
  data () {
    return {
      nickname: '游客',
      show: true,
      showEdit: false,
      srcUrl: 'http://liufusong.top:8080/img/profile/bg.png'
    }
  },
  async created () {
    const { body } = await getUserInfo()
    // console.log(res)
    if (getToken()) {
      this.username = body.nickname
      this.srcUrl = 'http://liufusong.top:8080/img/avatar.png'
      this.show = false
      this.showEdit = true
    }
  },
  methods: {
    clickFn () {
      // this.show = true
      this.$dialog.confirm({
        title: '提示',
        message: '是否确定退出',
        confirmButtonColor: '#21b97a'
      })
        .then(() => {
          // on confirm
          this.show = true
          this.showEdit = false
          this.srcUrl = 'http://liufusong.top:8080/img/profile/bg.png'
          this.username = '游客'
          logout()
          delToken()
          this.$router.push('/mymine')
        })
        .catch(() => {
          // on cancel
          this.show = false
        })
    }
  }
}
</script>

<style lang="less" scoped>

.user-page {
  .bg {
    min-height: 270px;
    position: relative;
    img {
      width: 100%;
    }
    .login {
      position: absolute;
      bottom: 0;
      left: 50%;
      transform: translateX(-50%);
      background-color: #fff;
      width: 75%;
      height: 60%;
      box-shadow: 0 0 10px 3px #ddd;
      margin: 50px auto 0;
      padding: 0 20px;
      text-align: center;
      font-size: 13px;
      .picture {
        position: relative;
        transform: translateY(-50%);
        border-radius: 50%;
        width: 60px;
        height: 60px;
        border: 5px solid #f5f5f5;
        display: inline-block;
        box-shadow: 0 2px 2px #bdbdbd;
        img {
          width: 100%;
          border-radius: 50%;
        }
      }
      .text {
        padding-top: 15px;
        p {
          margin-top: -40px;
        }
        .edit{
          display: flex;
          align-items: center;
          justify-content: center;
          margin-top: 30px;
          color: #b6b3b6;
          font-size: 12px;
          .angle{
            display: block;
            margin-top: 3px;
            margin-left: 8px;
            width: 0;
            height: 0;
            border: 5px solid transparent;
            border-left: 5px solid #b6b3b6;
          }
        }
      }
    }
  }
  .list{
    margin-top: 20px;
  }
  .more{
    margin-top: 20px;
    text-align: center;
    img{
      width: 92%;
    }
  }
}
</style>
